var url 		= "http://203.236.20.167:9000/service.pe?";
//var url 		= "http://203.236.20.112:9000/service.pe?";
var authInfo 	= null;
var xmlHandler 	= new SKTXmlHandler;
var dataSet 	= new DataSet();
var referencePanelHidden = 1;	//참조, 숨은참조 패널 보이고 숨기기
var reference_file = 1;
var key_theme 	= "theme";
var mailIdAr 	= null; //이전메일 다음메일 위한
var mailChangeKeyAr = null;
var mailIdIndex = null;
var mailId 	  	= null;
var changeKey 	= null;

function init() {
	
	nativeUI.showProgressDialog();
	
	checkTheme();		//배경테마 선택
	clickHandlerInit();	//이벤트핸들러 초기화
	setLocalMenu();		//menu 초기화
	
	// Mail View가 실행되며 넘겨받은 인자값 저장
	mailId 	  	= alopexController.parameters["mailId"];
	changeKey 	= alopexController.parameters["changeKey"];
	authInfo  		= alopexController.parameters["authInfo"];
	
	var mailIdString		= alopexController.parameters["mailIdAr"];
	var mailChangeKeyString = alopexController.parameters["mailChangeKeyAr"];
	mailIdIndex		= alopexController.parameters["mailIdIndex"];
	//alert( "authInfo : \n" + authInfo + "\n mailId : \n" + mailId + "\n changeKey : \n" + changeKey );
	//alert(mailIdIndex);
//	var tmpResult = "";
//	for(var i=0; i<mailIdAr.length; i++){tmpResult+=mailIdAr[i]};
//	alert(mailIdAr.length + " : " + tmpResult);
	
	if(mailIdString != undefined || mailIdString !=null)
		mailIdAr = mailIdString.split(',');
	if(mailChangeKeyString != undefined || mailChangeKeyString !=null)
		mailChangeKeyAr = mailChangeKeyString.split(',');
	
	
	if( authInfo==undefined || authInfo==null ){
		gmpUtil.getAuthInfo(authCallback, authErrorCallback);
	}
	
	requestMailContents();

	
}


//GMP AuthKey
function authCallback(successMessage){
	
	authInfo	 = successMessage;
	requestMailContents();
}

function requestMailContents(){
	var tmpParams = new Object();
	tmpParams["function"] 	= "COMMON_MAIL_CONTENT";
	tmpParams["ID"]		 	= URLEncode(mailId);
	tmpParams["CHANGEKEY"] 	= URLEncode(changeKey);
	tmpParams["attachmentInclude"] 	= "false"; //false가 기본, true시 content받아옴
//	tmpParams["attachmentInclude"] 	= "true";
	//... 메일본문의 Type -Text -HTML : 일단 text 로 구현 후 차후 html로 변경 태그관련 처리가 많음
	tmpParams["contentBodyType"] = URLEncode("HTML");
	
	var requestXml = xmlHandler.make(tmpParams);
	log.log( "MAIL VIEW Request :: " + url + "\n authInfo: " + authInfo + "\n requestXml: " + requestXml );
	//alert( "MAIL VIEW Request :: " + url + "\n authInfo: " + authInfo + "\n requestXml: " + requestXml );
	http.request(url + authInfo, "POST", tmpParams, getDetailCallback, failureCallback);
}

function authErrorCallback(errorMessage){

	if(errorMessage == "AuthError") {
		
		alert("로그인 정보가 없습니다. 로그인 하십시오.");
		gmpUtil.loginGmp(authCallback, authErrorCallback);
	
	} else {
	
		log.error("errorMessage :: " + errorMessage);
		alert("에러 : " + errorMessage );
		nativeUI.dismissProgressDialog();
		
	}
}

function URLDecode (encodedString) {
	  var output = encodedString;
	  var binVal, thisString;
	  var myregexp = /(%[^%]{2})/;
	  while ((match = myregexp.exec(output)) != null
	             && match.length > 1
	             && match[1] != '') {
	    binVal = parseInt(match[1].substr(1),16);
	    thisString = String.fromCharCode(binVal);
	    output = output.replace(match[1], thisString);
	  }
	  return output;
	}

function provisionCallback(registrationId, authToken){
	alert(registrationId);
	alert(authToken);
//	nativeUI.showProgressDialog();
//	var tmpParams = new Object();
//	tmpParams["function"] 	= "COMMON_MAIL_CONTENT";
//	tmpParams["ID"]		 	= URLEncode(mailIdAr[mailIdIndex]);
//	tmpParams["CHANGEKEY"] 	= URLEncode(mailChangeKeyAr[mailIdIndex]);
//	tmpParams["attachmentInclude"] 	= "true"; //false가 기본, true시 content받아옴
//	//... 메일본문의 Type -Text -HTML : 일단 text 로 구현 후 차후 html로 변경 태그관련 처리가 많음
//	tmpParams["contentBodyType"] = URLEncode("HTML");
//	
//	var requestXml = xmlHandler.make(tmpParams);
//	log.log( "MAIL VIEW Request :: " + url + "\n authInfo: " + authInfo + "\n requestXml: " + requestXml );
//	//alert( "MAIL VIEW Request :: " + url + "\n authInfo: " + authInfo + "\n requestXml: " + requestXml );
//	http.setTimeout(60000);
//	http.request(url + authInfo, "POST", tmpParams, downCallback, failureCallback);
	
}

function clickHandlerInit(){
	
	viewPanelBtn3.setOnClickHandler(imageDown);
	viewPanelBtn2.setOnClickHandler(referencePanelHiddenChange);
	viewPanelImg1.setOnClickHandler(unreadRequest);
	alopexController.setCustomizedBack(btnBackClick);
}

function getDetailCallback(){

	var xmlStr = http.response;	
	log.log("getDetailCallback :: " + xmlStr);
	//alert("getDetailCallback :: " + xmlStr);
	
    //result값이 정상인 "1000"이 넘어왔는지 확인
    var tmpxml = (new DOMParser()).parseFromString(xmlStr, "text/xml");
	var result = tmpxml.getElementsByTagName("result")[0].firstChild.nodeValue;
	var resultMessage = tmpxml.getElementsByTagName("resultMessage")[0].firstChild.nodeValue;
	if(result == "1000"){
		
//... sample - 첨부파일이 있는 경우 차후구현
//		<?xml version="1.0" encoding="utf-8" standalone="yes"?>
//			 <Response>
//			    <metaInfo>
//			         <primitive>COMMON_MAIL_CONTENT</primitive>
//			         <result>1000</result>
//			         <resultMessage>NoError</resultMessage>
//			    </metaInfo>
//				<list>
//			        <listCount>1</listCount>
//			        <listName>attachmentsList</listName>
//			        <item>
//			            <name>meritz.pdf</name>
//			            <id>AAAXAGdtcHRlc3QxQHBhcnRuZXIuc2suY29tAEYAAAAAACplDSJMPe1Fqk+vuVRZCfwHAAI9H29iww1Mhu8dkVMJYJ0AdGZ+4JgAAF68bG3xPGdNgglPYxgsE10AAAC6VMYAAAESABAAJmXDaoDtvki0GyESndkswQ==</id>
//			        </item>
//			    </list>
//				<data>
//			        <isRead>true</isRead>
//			        <sendDate>2011-09-26T16:54:34.000Z</sendDate>
//			        <receivedDate>2011-09-26T16:54:34.000Z</receivedDate>
//			        <hasAttachments>false</hasAttachments>
//			        <body>아자차카 </body>
//			
//			        <bodyType>Text</bodyType>
//			        <mailId>AAAXAGdtcHRlc3QxQHBhcnRuZXIuc2suY29tAEYAAAAAACplDSJMPe1Fqk+vuVRZCfwHAAI9H29iww1Mhu8dkVMJYJ0AdGZ+4JgAAF68bG3xPGdNgglPYxgsE10AAAC6VL0AAA==</mailId>
//			        <changeKey>CQAAABYAAABevGxt8TxnTYIJT2MYLBNdAAAAvIDN</changeKey>
//			        <mailTitle>가나다라마바사</mailTitle>
//			        <fromInfoName>Chang-Gi Lee</fromInfoName>
//			        <fromInfoEmail>cglee@ccfocusone.com</fromInfoEmail>
//			        <toListNames>gmptest1/Partner/SKCC;</toListNames>
//			        <toListEmails>gmptest1@partner.sk.com;</toListEmails>
//			        <ccListNames>jjinyo@hanmail.net;jaezinpark@gmail.com;</ccListNames>
//			        <ccListEmails>jjinyo@hanmail.net;jaezinpark@gmail.com;</ccListEmails>
//			  </data>
//			<Response>
			
		//dataSet.get("isRead");		//읽음유무 - 처리없음
		//dataSet.get("sendDate");		//보낸날짜 - 처리없음
		//dataSet.get("receivedDate");	//받은날짜
		//dataSet.get("hasAttachments");//첨부파일유무
		//dataSet.get("body");			//메일본문
		//dataSet.get("bodyType");		//메일본문타입 - 처리없음
		//dataSet.get("mailId");		//메일ID	-삭제, 안읽음처리시 사용
		//dataSet.get("changeKey");		//메일key	-삭제, 안읽음처리시 사용
		//dataSet.get("mailTitle");		//메일제목
		//dataSet.get("fromInfoName");	//보낸이
		//dataSet.get("fromInfoEmail");	//보낸이주소 - 처리없음
		//dataSet.get("toListNames");	//받는이
		//dataSet.get("toListEmails");	//받는이주소 - 처리없음
		//dataSet.get("ccListNames");	//참조인
		//dataSet.get("ccListEmails");	//참조인주소 - 처리없음
		
		//메일 상세보기 데이터 바인딩
		dataSet= xmlHandler.parse(xmlStr);
		subject.setText(dataSet.get("mailTitle"));
		viewPanelTxtFeild1.setText(dataSet.get("receivedDate"));
		createdInfo2.setText(dataSet.get("fromInfoName"));
		statusLabel.setText(dataSet.get("body"));

		//...이전메일, 다음메일 있는경우 버튼 처리
		//alert( mailIdIndex + " " + (mailIdAr.length-1) );
		//mailIdIndex가 0이면 이전메일은 없다. mailIdIndex가 mailIdAr.length-1와 같으면 다음메일은 없다.
		if(0<mailIdIndex){
			//...
			document.getElementById("downBtn").style.visibility="visible";
			downBtn.setOnClickHandler(downBtnHandler);
			
		}else{
			document.getElementById("downBtn").style.visibility="hidden";
		};
		
		if(mailIdIndex<mailIdAr.length-1){
			document.getElementById("upBtn").style.visibility="visible";
			upBtn.setOnClickHandler(upBtnHandler);
			
		}else{
			document.getElementById("upBtn").style.visibility="hidden";
		}
		
		//받는이 정보 및 배열관리
		if(dataSet.get("toListNames")!=undefined){
//			refInfo1.setText(dataSet.get("toListNames"));
			var toListNamesAr   = dataSet.get("toListNames").split(';');
			var toListEmailsAr  = dataSet.get("toListEmails").split(';');
			var toListAr		= new Array();	//받는이 이름, 이메일 배열
			
			for(var k=0; k<toListEmailsAr.length; k++){
				if(toListEmailsAr[k]==""){
					toListNamesAr.remove(k);
					toListEmailsAr.remove(k); 
				}
			};
			
			for(var k=0; k<toListEmailsAr.length; k++){
				//alert( toListNamesAr[k] + " : " + toListEmailsAr[k] );
				var tmpToListAr = new Map();
				tmpToListAr.put("UserName",toListNamesAr[k]);
				tmpToListAr.put("UserMail",toListEmailsAr[k]);
				toListAr.push(tmpToListAr);
			};
			userList.setData(toListAr);
		}
		
		//참조 정보 및 배열관리
		if(dataSet.get("ccListNames")!=undefined){
//			hideInfo2.setText(dataSet.get("ccListNames"));
			var ccListNamesAr  = dataSet.get("ccListNames").split(';');
			var ccListEmailsAr = dataSet.get("ccListEmails").split(';');
			var ccListAr 	   = new Array(); //참조자 이름, 이메일 배열
			
			for(var k=0; k<ccListEmailsAr.length; k++){
				if(ccListEmailsAr[k]==""){
					ccListNamesAr.remove(k);
					ccListEmailsAr.remove(k); 
				}
			};
			
			for(var k=0; k<ccListEmailsAr.length; k++){
				//alert( ccListNamesAr[k] + " : " + ccListEmailsAr[k] );
				var tmpCcListAr = new Map();
				tmpCcListAr.put("UserName",ccListNamesAr[k]);
				tmpCcListAr.put("UserMail",ccListEmailsAr[k]);
				ccListAr.push(tmpCcListAr);
			};
			refList.setData(ccListAr);
		}else{
			
			document.getElementById("viewPanel21_2").style.display = "none";
			
		}
		
		//첨부파일리스트
		if( dataSet.get("hasAttachments") == "true" ){
			document.getElementById("fileBtn1").style.display = "block";
			
			var tmpDataSet = new Array();
			for( var i = 0; i < dataSet.getList("attachmentsList").length; i++){
				
				var tmpNameAr = dataSet.getList("attachmentsList")[i].get("name").split('.');
				var tmpFileName 	 = "";
				var tmpFileExtension = "";
				for(var j=1; j<tmpNameAr.length; j++){
					tmpFileName += tmpNameAr[j-1]+".";
					tmpFileExtension = tmpNameAr[j];
				}
				
				var tmpMap = new Map();
				tmpMap.put("file1",tmpFileName);
				tmpMap.put("file2",tmpFileExtension);
				tmpDataSet.push(tmpMap);
			};
			//...
			fileBtn1.setOnClickHandler(attachmentsfileListOpen);
			fileList.setOnItemClickHandler(onFileListItemClick);
			fileList.setData(tmpDataSet);
			fileList.itemFocusColor = "transparent";
			
		}else{
			document.getElementById("fileBtn1").style.display = "none";
		}
		
	} else {
		if( confirm("처리에러\n Result Code : " + result + "\nresultMessage : \n" + resultMessage + "\n\n재시도 하시겠습니까?")){
			init();
		}else{
			nativeUI.dismissProgressDialog();
		};
	}
	
    nativeUI.dismissProgressDialog();
    scrollContainer.refresh();
}

//...첨부파일 다운로드
function onFileListItemClick(e){
	
	var tmpFileIndex = e.currentTarget.index;
	var tmpFileName  = dataSet.getList("attachmentsList")[tmpFileIndex].get("name");
	var tmpFileId 	 = dataSet.getList("attachmentsList")[tmpFileIndex].get("id");
	
	if(confirm(tmpFileName + "  파일을\n다운로드 하시겠습니까?")){

		nativeUI.showProgressDialog();
		var tmpParams = new Object();
		tmpParams["function"] 	  = "COMMON_MAIL_ATTACHMENT";
		tmpParams["AttachmentId"] = URLEncode(tmpFileId);

		var requestXml = xmlHandler.make(tmpParams);
		log.log("requestXml :: " + url + authInfo + requestXml);
		
		http.setTimeout(60000);
		http.request( url + authInfo, "POST", tmpParams, getMailAttachmentCallback, failureCallback );
	};
}
//function downCallback(){
//	var xmlStr = http.response;	
//	var lotmpDataSet   	= xmlHandler.parse(xmlStr);
////	var result = parseXml( xmlStr , "result" );
////	if(result == "1000") {
////		alert("b");
//		for(var i=0; i<lotmpDataSet.getList("attachmentsList").length; i++){
//			var content	= lotmpDataSet.getList("attachmentsList")[i].get("content");
//			var fileName = lotmpDataSet.getList("attachmentsList")[i].get("name");
//			if(content != null && content != ""){
//				gmpUtil.writeFile(fileName, content);
//			}
//		}
////	}
//	nativeUI.dismissProgressDialog();
//    scrollContainer.refresh();
//}
function getMailAttachmentCallback(){

	var xmlStr = http.response;	
	log.log("getDetailCallback :: " + xmlStr);
	
    //result값이 정상인 "1000"이 넘어왔는지 확인
    var tmpxml = (new DOMParser()).parseFromString(xmlStr, "text/xml");
    log.log(result);
    
	var result = tmpxml.getElementsByTagName("result")[0].firstChild.nodeValue;
	if(result == "1000"){
		
		var fileDataSet 	= new DataSet();
		fileDataSet= xmlHandler.parse(xmlStr);
		//alert("다운로드된 파일입니다.\n파일처리는 차후 업데이트 예정입니다.\n"+fileDataSet.get("content"));
		
		var content	= fileDataSet.get("content");
		var fileName = fileDataSet.get("name");
		gmpUtil.writeFile(fileName, content);
		
	} else {
		alert("서버에러코드 : " + result);
	}
	
    nativeUI.dismissProgressDialog();
}

//array 삭제기능
Array.prototype.remove = function(object) {
  var from = this.indexOf(object);
  var to = from;
  var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};

function failureCallback() {
	log.error("Http Error:" + http.error);
	nativeUI.dismissProgressDialog();
	alert("통신상태 확인이 필요합니다. \n" + http.error );
}

//안읽음처리 요청
function unreadRequest(){

		nativeUI.showProgressDialog();
		var tmpParams = new Object();
		tmpParams["function"] 	= "COMMON_MAIL_ISREAD";
		tmpParams["ID"] 		= URLEncode(dataSet.get("mailId"));
		tmpParams["CHANGEKEY"]	= URLEncode(dataSet.get("changeKey"));
		tmpParams["ISREAD"]		= "false";
		
		//alert(tmpParams["ID"] + " : " + tmpParams["CHANGEKEY"])

		var requestXml = xmlHandler.make(tmpParams);
		log.log("requestXml :: " + url + authInfo + requestXml);
		http.request( url + authInfo, "POST", tmpParams, getMailUnReadCallback, failureCallback );
	
}

//메일삭제요청 전문 메일id와 changekey로 요청
function mailDelRequest(){
	
		nativeUI.showProgressDialog();
		var tmpParams = new Object();
		tmpParams["function"] 	= "COMMON_MAIL_DELETE";
		tmpParams["ID"] 		= URLEncode(dataSet.get("mailId"));
		tmpParams["CHANGEKEY"]	= URLEncode(dataSet.get("changeKey"));
		tmpParams["isHardDel"]	= "false";
		
		//alert(tmpParams["ID"] + " : " + tmpParams["CHANGEKEY"])
	
		var requestXml = xmlHandler.make(tmpParams);
		log.log("requestXml :: " + url + authInfo + requestXml);
		http.request( url + authInfo, "POST", tmpParams, getMailUnReadCallback, failureCallback );
}

//안읽음표시,삭제요청 성공시 CallBack
function getMailUnReadCallback(){
	
	var xmlStr = http.response;	
	log.log("MailDel SuccessResponseXml :: \n" + xmlStr);
	//alert("MailDel SuccessResponseXml :: \n" + xmlStr);
	nativeUI.dismissProgressDialog();
	alopexController.goStartPage();

}

//참조, 숨은참조 패널 보이고 숨기기
function referencePanelHiddenChange(){

	if( referencePanelHidden == 1 ){
	document.getElementById("viewPanel21").style.display = "block";
	document.getElementById("viewPanelBtn2").className = "ButtonOpen";
	document.getElementById("viewPanel2").className = "viewPanelDl open";
	scrollContainer.refresh();
	referencePanelHidden=0;

	}
	
	else if(referencePanelHidden==0){
	document.getElementById("viewPanel21").style.display = "none";
	document.getElementById("viewPanel2").className = "viewPanelDl";
	scrollContainer.refresh();
	referencePanelHidden=1;

	}
}

//첨부파일리스트 보이고숨기기
function attachmentsfileListOpen(){

	if(reference_file==1){
		document.getElementById("fileBtn1").className = "fileOpen";
		document.getElementById("viewPanel5").style.display = "block";
		
		scrollContainer.refresh();
		reference_file=0;

	}
	else if(reference_file==0){
		document.getElementById("viewPanel5").style.display = "none";
		document.getElementById("fileBtn1").className = "file";
		
		scrollContainer.refresh();
		reference_file=1;

	}
	
}

//...이전메일 다음메일
function downBtnHandler(){	
	
	nativeUI.showProgressDialog();
	mailIdIndex--;
	var tmpParams = new Object();
	tmpParams["function"] 	= "COMMON_MAIL_CONTENT";
	tmpParams["ID"]		 	= URLEncode(mailIdAr[mailIdIndex]);
	tmpParams["CHANGEKEY"] 	= URLEncode(mailChangeKeyAr[mailIdIndex]);
	tmpParams["attachmentInclude"] 	= "false"; //false가 기본, true시 content받아옴
	//... 메일본문의 Type -Text -HTML : 일단 text 로 구현 후 차후 html로 변경 태그관련 처리가 많음
	tmpParams["contentBodyType"] = URLEncode("HTML");
	
	var requestXml = xmlHandler.make(tmpParams);
	log.log( "MAIL VIEW Request :: " + url + "\n authInfo: " + authInfo + "\n requestXml: " + requestXml );
	//alert( "MAIL VIEW Request :: " + url + "\n authInfo: " + authInfo + "\n requestXml: " + requestXml );
	
	http.request(url + authInfo, "POST", tmpParams, getDetailCallback, failureCallback);
	
}
function upBtnHandler(){
	
	nativeUI.showProgressDialog();
	mailIdIndex++;
	var tmpParams = new Object();
	tmpParams["function"] 	= "COMMON_MAIL_CONTENT";
	tmpParams["ID"]		 	= URLEncode(mailIdAr[mailIdIndex]);
	tmpParams["CHANGEKEY"] 	= URLEncode(mailChangeKeyAr[mailIdIndex]);
	tmpParams["attachmentInclude"] 	= "false"; //false가 기본, true시 content받아옴
	//... 메일본문의 Type -Text -HTML : 일단 text 로 구현 후 차후 html로 변경 태그관련 처리가 많음
	tmpParams["contentBodyType"] = URLEncode("HTML");
	
	var requestXml = xmlHandler.make(tmpParams);
	log.log( "MAIL VIEW Request :: " + url + "\n authInfo: " + authInfo + "\n requestXml: " + requestXml );
	//alert( "MAIL VIEW Request :: " + url + "\n authInfo: " + authInfo + "\n requestXml: " + requestXml );
	
	http.request(url + authInfo, "POST", tmpParams, getDetailCallback, failureCallback);

}
//...
function imageDown(){
	gmpUtil.provision(provisionCallback, failureCallback);
//	nativeUI.showProgressDialog();
//	var tmpParams = new Object();
//	tmpParams["function"] 	= "COMMON_MAIL_CONTENT";
//	tmpParams["ID"]		 	= URLEncode(mailIdAr[mailIdIndex]);
//	tmpParams["CHANGEKEY"] 	= URLEncode(mailChangeKeyAr[mailIdIndex]);
//	tmpParams["attachmentInclude"] 	= "true"; //false가 기본, true시 content받아옴
//	//... 메일본문의 Type -Text -HTML : 일단 text 로 구현 후 차후 html로 변경 태그관련 처리가 많음
//	tmpParams["contentBodyType"] = URLEncode("HTML");
//	
//	var requestXml = xmlHandler.make(tmpParams);
//	log.log( "MAIL VIEW Request :: " + url + "\n authInfo: " + authInfo + "\n requestXml: " + requestXml );
//	//alert( "MAIL VIEW Request :: " + url + "\n authInfo: " + authInfo + "\n requestXml: " + requestXml );
//	http.setTimeout(60000);
//	http.request(url + authInfo, "POST", tmpParams, downCallback, failureCallback);
}

function downCallback(){
	var xmlStr = http.response;	
	var lotmpDataSet   	= xmlHandler.parse(xmlStr);
//	var result = parseXml( xmlStr , "result" );
//	if(result == "1000") {
//		alert("b");
		for(var i=0; i<lotmpDataSet.getList("attachmentsList").length; i++){
			var content	= lotmpDataSet.getList("attachmentsList")[i].get("content");
			var fileName = lotmpDataSet.getList("attachmentsList")[i].get("name");
			if(content != null && content != ""){
				gmpUtil.writeFile(fileName, content);
			}
		}
//	}
	nativeUI.dismissProgressDialog();
    scrollContainer.refresh();
}

function setLocalMenu(){
	menu.removeAll();
	menu.add("답장", replyMail, "");
	menu.add("전체답장", replyallMail, "");
	menu.add("삭제", mailDelRequest, "");
	menu.add("전달", forwardMail, "");
	menu.add("메일작성", writeMail, "");
}

//dataSet.get("isRead");		//읽음유무 - 처리없음
//dataSet.get("sendDate");		//보낸날짜 - 처리없음
//dataSet.get("receivedDate");	//받은날짜
//dataSet.get("hasAttachments");//첨부파일유무
//dataSet.get("body");			//메일본문
//dataSet.get("bodyType");		//메일본문타입 - 처리없음
//dataSet.get("mailId");		//메일ID	-삭제, 안읽음처리시 사용
//dataSet.get("changeKey");		//메일key	-삭제, 안읽음처리시 사용
//dataSet.get("mailTitle");		//메일제목
//dataSet.get("fromInfoName");	//보낸이
//dataSet.get("fromInfoEmail");	//보낸이주소 - 처리없음
//dataSet.get("toListNames");	//받는이
//dataSet.get("toListEmails");	//받는이주소 - 처리없음
//dataSet.get("ccListNames");	//참조인
//dataSet.get("ccListEmails");	//참조인주소 - 처리없음

//답장
function replyMail() {
	
	alopexController.navigateWithParameters("Mail_Write", {
		"authInfo" : authInfo, "mailId" : dataSet.get("mailId"), "changeKey" : dataSet.get("changeKey"),
		"fromInfoName" : dataSet.get("fromInfoName"), "fromInfoEmail" : dataSet.get("fromInfoEmail"),
		"ccListNames" : dataSet.get("ccListNames"), "ccListEmails" : dataSet.get("ccListEmails"),
		"mailTitle" : dataSet.get("mailTitle"), "mailType" : "reply"
	});
}

//전체답장
function replyallMail() {
	//보낸이가 받는이가 된다.
	//참조가 모두 참조가 된다.
	//메일 제목에 Re: 를 붙인다
	alopexController.navigateWithParameters("Mail_Write", {
		"authInfo" : authInfo, "mailId" : dataSet.get("mailId"), "changeKey" : dataSet.get("changeKey"),
		"fromInfoName" : dataSet.get("fromInfoName"), "fromInfoEmail" : dataSet.get("fromInfoEmail"),
		"toListNames" : dataSet.get("toListNames"), "toListEmails" : dataSet.get("toListEmails"),
		"ccListNames" : dataSet.get("ccListNames"), "ccListEmails" : dataSet.get("ccListEmails"),
		"mailTitle" : dataSet.get("mailTitle"), "mailType" : "replyAll"
	});
}

//메일전달
function forwardMail() {
	//메일 제목에 Fwd: 를 붙인다
	alopexController.navigateWithParameters("Mail_Write", {
		"authInfo" : authInfo, "mailId" : dataSet.get("mailId"), "changeKey" : dataSet.get("changeKey"),
		"mailTitle" : dataSet.get("mailTitle"), "mailType" : "forward"
	});
	
}

//메일쓰기
function writeMail() {
	
	alopexController.navigateWithParameters("Mail_Write",{"authInfo":authInfo});
}



function checkTheme(){
	
	var selectTheme = preference.getString(key_theme);
	
	if ( selectTheme == "black" ){
		document.getElementById("themePanel").className = "themePanel_b";
		
	} else if ( selectTheme == "white" ){
		document.getElementById("themePanel").className = "themePanel_w";
		
	} 
	
}

function btnBackClick(){
	
//	alopexController.navigateWithParameters("Mail_List_biz", {"authInfo" : authInfo});
	alopexController.goStartPage();
	//alopexController.setCustomizedBack(btnBackClick);
	
}

function URLEncode (clearString) {
	  var output = '';
	  var x = 0;
	  clearString = clearString.toString();
	  var regex = /(^[a-zA-Z0-9_.]*)/;
	  while (x < clearString.length) {
	    var match = regex.exec(clearString.substr(x));
	    if (match != null && match.length > 1 && match[1] != '') {
	    output += match[1];
	      x += match[1].length;
	    } else {
	      if (clearString[x] == ' ')
	        output += '+';
	      else {
	        var charCode = clearString.charCodeAt(x);
	        var hexVal = charCode.toString(16);
	        output += '%' + hexVal.toUpperCase();
	      }
	      x++;
	    }
	  }
	  return output;
}